<?php
/**
 * 功能：后台首页控制器
 * 作者：魏安来
 * 日期：2017/12/12
 */

namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cookie;
use think\Session;

class Login extends Controller{
	/*登录*/
	public function index(){
		if (request()->isPost()) {
			//接收字段
			$data = input('post.');
			unset($data['remember']);
			//验证字段
			$validate = validate('User');
			$result = $validate->scene('login')->check($data);
			if ($result) {
				//对比数据库验证数据
				$data['password'] = sha1(input('password'));
				$userInfo = Db::name('user')->where($data)->find();
				if ($userInfo) {
					$uid = Db::name('user')->where('username', input('username'))->where('password', $data['password'])->find();
					//判断用户是否记住密码
					if (input('remember') == 'on') {
						//写入cookie
						Cookie::set('username', input('username'), ['expire'=>3600*24*365]);
						Cookie::set('password', input('password'), ['expire'=>3600*24*365]);
						//写入session
						Session::set('uid', $uid['id'], 'tp');
					} else {
						//如果cookie存在删除cookie
						Cookie::delete('username');
						Cookie::delete('password');
						//写入session
						Session::set('uid', $uid['id'], 'tp');
					}
					$this->success('登录成功!', '/');
				} else {
					$this->error('用户名或密码不正确!');
				}
			} else {
				$this->error($validate->getError());
			}
		}
		return $this->fetch();
	}
}
	
?>